'use strict';
module.exports = {
	/**
	 * 此函数名称
	 * @url client/user/pub/test 前端调用的url参数地址
	 * data 请求参数
	 * @params {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, originalParam } = event;
		let { customUtil, config, pubFun, vk, db, _ } = util;
		let { uid, like_state, user_id, nickname, tv_name, tv_id, tv_image, series, series_id } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始-----------------------------------------------------------
		// 可写与数据库的交互逻辑等等
		console.log("like_state=", like_state);
		//like_state代表是点赞还是取消点赞，点赞是false,取消是true
		if (!like_state) {
			let like_count = await db.collection('a_like').where({
				user_id: user_id,
				series_id: series_id
			}).count()
			console.log("like_count=", like_count.total);
			if (like_count.total == 0) {
				const add1 = await db.collection('a_like').add({
					user_id: user_id,
					nickname: nickname,
					tv_name: tv_name,
					tv_id: tv_id,
					series: series,
					tv_image: tv_image,
					series_id: series_id,
					_add_time: new Date().getTime(),
					_add_time_str: vk.pubfn.timeFormat(new Date().getTime(), "yyyy-MM-dd hh:mm:ss")
				})

				const add2 = await db.collection('a_tv_series').where({ _id: series_id }).update({
					//该条视频总点赞量加1
					like_num: _.inc(1)
				})
				console.log("点赞完成");
			}
		} else {
			db.collection('a_like').where({
				series_id: series_id
			}).remove()
			console.log("删除完成");
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
